package com.zwkj.service.impl;

import cn.hutool.core.io.file.FileNameUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zwkj.domain.WFileT;
import com.zwkj.domain.WFileVersionT;
import com.zwkj.domain.WUserAclT;
import com.zwkj.domain.WUserT;
import com.zwkj.mapper.WFileTMapper;
import com.zwkj.mapper.WUserAclTMapper;
import com.zwkj.pojo.vo.*;
import com.zwkj.service.*;
import com.zwkj.util.RequestUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.List;

/**
 * @author Gap
 * @version 1.0
 * @createDate 时间:2023/12/11
 * @description 描述:
 */
@Service
public class PreviewWpsServiceImpl implements IPreviewWpsService {
    @Autowired
    private IPreviewWpsService previewWpsService;
    @Autowired
    private IWFileTService fileTService;
    @Autowired
    private WUserAclTMapper wUserAclTMapper;
    @Autowired
    private WFileTMapper fileTMapper;
    @Autowired
    private IWUserTService userTService;
    @Autowired
    private IWFileVersionTService fileVersionTService;
    @Autowired
    private IWUserAclTService userAclTService;

    @Value("${wps.host}")
    private String host;

    @Override
    public FileVo getFileById(String fileId) {
        WFileT wFileT = fileTService.getById(fileId);
        if (wFileT == null) {
            return null;
        }
        FileVo fileVo = new FileVo();
        fileVo.setId(String.valueOf(wFileT.getId()));
        fileVo.setName(wFileT.getName());
        fileVo.setVersion(wFileT.getVersion());
        fileVo.setSize(Math.toIntExact(wFileT.getSize()));
        fileVo.setCreateTime(Math.toIntExact(wFileT.getCreateTime().toEpochSecond(ZoneOffset.UTC)));
        fileVo.setModifyTime(Math.toIntExact(wFileT.getModifyTime().toEpochSecond(ZoneOffset.UTC)));
        fileVo.setCreatorId(wFileT.getCreator());
        fileVo.setModifierId(wFileT.getModifier());
        return fileVo;
    }

    @Override
    public FileVo getFileDownloadUrlById(String fileId, String type) {
        WFileT wFileT = fileTService.getById(fileId);
        if (wFileT == null) {
            return null;
        }
        FileVo fileVo = new FileVo();
        fileVo.setId(String.valueOf(wFileT.getId()));
        fileVo.setName(wFileT.getName());
        String url = host + "/console/download/" + fileId + "/" + wFileT.getVersion() + "/" + type;
        fileVo.setUrl(url);
        return fileVo;
    }

    @Override
    public UserPermissionVo getUserPermissionByFileId(String fileId) {
        String userId = RequestUtils.getWebOfficeToken(RequestUtils.getCurrentRequest());
        UserPermissionVo userAclTVo = wUserAclTMapper.getUserPermissionByFileId(fileId, userId);
        return userAclTVo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveNewUserAndFile(FileParmsVo fileParmsVo) {
        //检查人员
        WUserT wUserT = userTService.getById(fileParmsVo.getUserId());
        if (wUserT == null) {
            WUserT userT = new WUserT();
            userT.setId(Long.valueOf(fileParmsVo.getUserId()));
            userT.setName(fileParmsVo.getUserName());
            userTService.save(userT);
        }
        //检查文件
        WFileT fileT = fileTService.getOne(new QueryWrapper<WFileT>().eq("table_file_id", fileParmsVo.getTableFileId()));
        if (fileT == null) {
            String extName = FileNameUtil.extName(fileParmsVo.getDownloadUrl());
            String mainName = FileNameUtil.mainName(fileParmsVo.getDownloadUrl());
            WFileT fl = new WFileT();
            fl.setName(mainName + "." + extName);
            fl.setVersion(1);
            fl.setSize(1000L);
            fl.setDownloadUrl(fileParmsVo.getDownloadUrl());
            fl.setCreateTime(LocalDateTime.now());
            fl.setModifyTime(LocalDateTime.now());
            fl.setCreator(fileParmsVo.getUserId());
            fl.setModifier(fileParmsVo.getUserId());
            fl.setTableFileId(Long.valueOf(fileParmsVo.getTableFileId()));
            fileTService.save(fl);
            //历史信息表
            WFileVersionT versionT = new WFileVersionT();
            versionT.setFileId(Long.valueOf(fl.getId()));
            versionT.setName(mainName + "." + extName);
            versionT.setVersion(1);
            versionT.setDownloadUrl(fileParmsVo.getDownloadUrl());
            versionT.setSize(1000L);
            versionT.setCreateTime(LocalDateTime.now());
            versionT.setModifyTime(LocalDateTime.now());
            versionT.setCreator(fileParmsVo.getUserId());
            versionT.setModifier(fileParmsVo.getUserId());
            fileVersionTService.save(versionT);
            //用户文件权限表
            WUserAclT userAclT = new WUserAclT();
            userAclT.setUserId(Long.valueOf(fileParmsVo.getUserId()));
            userAclT.setFileId(Long.valueOf(fl.getId()));
            if (fileParmsVo.getRead() != null) {
                userAclT.setUserRead(fileParmsVo.getRead());
            }
            if (fileParmsVo.getUpdate() != null) {
                userAclT.setUserUpdate(fileParmsVo.getUpdate());
            }
            if (fileParmsVo.getDownload() != null) {
                userAclT.setUserDownload(fileParmsVo.getDownload());
            }
            userAclTService.save(userAclT);

        }
    }
}
